package com.nice.nice201110.service;

import com.nice.nice201110.entity.TLog;
import com.nice.nice201110.entity.TUser;
import com.nice.nice201110.mapper.TLogMapper;
import com.nice.nice201110.mapper.TUserMapper;
import com.nice.nice201110.utils.NiceTableEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

@Service
public class TuserService {

    @Autowired
    TUserMapper tUserMapper;

    @Autowired
    TLogMapper tLogMapper;

    @Autowired
    private PlatformTransactionManager transactionManager;

    private TLog tLog = null;

    /** 查询数据 */
    public List<TUser> getList(BaseVo baseVo){
        return tUserMapper.selectByPrimaryKeys(baseVo);
    }
    /** 单一查询  */
    public TUser getOne(UserVo userVo, Integer id){
        TUser tUser = tUserMapper.selectByPrimaryKey(id);
        if(tUser!=null){
            tLog = new TLog();
            tLog.setDate(new Date());
            tLog.setType(3);  //操作类型（新增0，修改1，删除2,查询3）
            tLog.setDocid(tUser.getId());
            tLog.setUsername(userVo.getName());
            tLog.setUserid(Integer.valueOf(userVo.getId()));
            tLog.setRemark("查看用户信息:"+tUser.toString());
            tLog.setTtable(NiceTableEnum.form_user);
            tLogMapper.insertSelective(tLog);
        }
        return tUser;
    }
    /** 插入信息 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean insertTUser(UserVo userVo,TUser tUser){
        try {
            tUser.setDate(new Date());
            int num = tUserMapper.insertSelective(tUser);
            if(num>0){
                tLog = new TLog();
                tLog.setDate(new Date());
                tLog.setType(0);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tUser.getId());
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_user);
                tLog.setRemark("新增用户信息:"+userVo.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;

    }
    /** 更新信息 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean updateTUser(UserVo userVo,TUser tUser){
        try {
            int num = tUserMapper.updateByPrimaryKeySelective(tUser);
            if(num>0){
                tLog = new TLog();
                tLog.setDate(new Date());
                tLog.setType(1);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tUser.getId());
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_user);
                tLog.setRemark("更新用户信息:"+userVo.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;

    }

    /** 删除信息 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean deleteTUser(UserVo userVo, String ids){
        try {
            String arr[] = ids.split(",");
            for(String idStr:arr) {
                if(idStr!=null&&idStr.trim().length()>0) {
                    int id = Integer.parseInt(idStr);
                    int num = tUserMapper.deleteByPrimaryKey(id);
                    if (num > 0) {
                        tLog = new TLog();
                        tLog.setDate(new Date());
                        tLog.setType(2);  //操作类型（新增0，修改1，删除2）
                        tLog.setDocid(id);
                        tLog.setUsername(userVo.getName());
                        tLog.setUserid(Integer.valueOf(userVo.getId()));
                        tLog.setTtable(NiceTableEnum.form_user);
                        tLog.setRemark("删除用户信息:" + tUserMapper.selectByPrimaryKey(id).toString());
                        tLogMapper.insertSelective(tLog);
                    } else {
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
}
